<?php
namespace app\admin\controller;
use function MongoDB\BSON\toJSON;
use think\Db;
use clt\Leftnav;
use app\admin\model\Admin;
use app\admin\model\AuthGroup;
use app\admin\model\authRule;
use think\facade\Request;
use think\Validate;
use app\admin\model\Staff as StaffModel;
class Staff extends Common
{
    //管理员列表
    public function staffList(){

        if(Request::isAjax()){
            $val=input('val');
            $url['val'] = $val;
            $this->assign('testval',$val);
            $map='';

//            if($val){
//                $map['username|email|tel']= array('like',"%".$val."%");
//            }
//            if (session('aid')!=1){
//                $map='admin_id='.session('aid');
//            }
            $list = StaffModel::where($map)->select();

            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list,'rel'=>1];
        }
        return view();
    }

    public function staffAdd(){
        if(Request::isAjax()){
            $data = input('post.');
            $check_user = StaffModel::get(['name'=>$data['name']]);
            if ($check_user) {
                return $result = ['code'=>0,'msg'=>'员工姓名已存在!'];
            }
            $data['author'] = session('aid');
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['updated_at'] = date('Y-m-d H:i:s');

            //添加
            if (StaffModel::create($data)) {
                return ['code'=>1,'msg'=>'员工添加成功!','url'=>url('staffList')];
            } else {
                return ['code'=>0,'msg'=>'员工添加失败!'];
            }
        }else{
            $this->assign('info',[]);
            return view('staffForm');
        }
    }
    //删除管理员
    public function staffDel(){
        $admin_id=input('post.id');
        if (session('aid')==1){
            StaffModel::where('id','=',$admin_id)->delete();
            return $result = ['code'=>1,'msg'=>'删除成功!'];
        }else{
            return $result = ['code'=>0,'msg'=>'您没有删除管理员的权限!'];
        }
    }
    //修改管理员状态
    public function staffState(){
        $id=input('post.id');
        $is_open=input('post.is_open');
        if (empty($id)){
            $result['status'] = 0;
            $result['info'] = '员工不存在!';
            $result['url'] = url('adminList');
            return $result;
        }
        db('staff')->where('id='.$id)->update(['is_show'=>$is_open,'updated_at'=>date('Y-m-d H:i:s')]);
        $result['status'] = 1;
        $result['info'] = '员工状态修改成功!';
        $result['url'] = url('staffList');
        return $result;
    }
    //更新管理员信息
    public function staffEdit(){
        if(request()->isPost()){
            $data = input('post.');
            $where['id'] = $data['id'];

            StaffModel::update($data,$where);

            return $result = ['code'=>1,'msg'=>'员工修改成功!','url'=>url('staffList')];
        }else{

            $model = new StaffModel();
            $info = $model->getInfo(input('id'));
            $this->assign('info', json_encode($info,true));
            $this->assign('title',"员工编辑");
            return view('staffForm');
        }
    }
}